package com.study.lambda;

import org.junit.Test;

import java.util.Comparator;
import java.util.function.Consumer;

/**
 * lambda表达式需要函数式接口支持
 * 函数式接口:接口中只有一个抽象方法称为函数式接口，可以使用@FunctionInterface来修饰，作用是检查是否是函数式接口
 * */
public class TestLambda1 {

    /**
     * 无参无返回值
     * */
    @Test
    public void test1(){
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello word");
            }
        };
        runnable.run();
        System.out.println("=========================");
        Runnable runnable1=()-> System.out.println("Hello Lambda");
        runnable1.run();
    }

    /**
     * 有一个参数没有返回值
     * */
    @Test
    public void test2(){
        Consumer<String> consumer=(a)-> System.out.println(a);
        consumer.accept("Hello Word");
    }

    /**
     * 有多个参数并且有多条语句
     * */
    @Test
    public void test3(){
        Comparator<Integer> comparable=(x, y)->{
            System.out.println("lambda test");
            return Integer.compare(x,y);
        };
        System.out.println(comparable.compare(1, 2));
    }

    /**
     * 有多个参数，有返回值，只有一条语句,{}和return都可以不写
     * */
    @Test
    public void test4(){
        Comparator<Integer> comparator=(x,y)->Integer.compare(x,y);
        System.out.println(comparator.compare(1, 2));
    }

    /**
     * 操作
     * */
    @Test
    public void test5(){
        System.out.println(operation(100, (x) -> x + 100));
        System.out.println("==================================");
        System.out.println(operation(200, (y) -> y * y));
    }

    public Integer operation(Integer num,MyFun<Integer> fun){
        return fun.operation(num);
    }


}
